﻿using furion.blog.Application.Contracts;
using furion.blog.Domain.Shared.Attribute;
using furion.blog.Domain.Shared.Dto.Dept;
using furion.blog.Utils;
using Furion.DynamicApiController;
using Microsoft.AspNetCore.Mvc;

namespace furion.blog.Web.Entry.Api
{
    /// <summary>
    /// 后台角色
    /// </summary>
    [ApiDescriptionSettings(Groups = new string[] { "DeptApi" })]
    [Route("api/dept")]
    public class DeptApiController : IDynamicApiController
    {
        private readonly IDeptService _deptService;
        public DeptApiController(IDeptService deptService)
        {
            this._deptService = deptService;
        }


        [ActionAttribute(Name = "部门树接口", Desc = "所有部门树")]
        [HttpGet,Route("tree"), Route("tree/{parentId}")]
        public async Task<IActionResult> tree([FromRoute(Name = "parentId")]long? parentId = 0)
        {
            var ret = await this._deptService.Tree(parentId);
            return new OkObjectResult(ResultUtil.Success(ret));
        }


        [ActionAttribute(Name = "部门信息接口", Desc = "获取部门信息")]
        [HttpGet, Route("{id}")]
        public async Task<IActionResult> detail([FromRoute(Name = "id")]long id) {
            var ret = await this._deptService.Detail(id);
            return new OkObjectResult(ResultUtil.Success(ret));
        }

        /// <summary>
        /// 新增部门信息
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        [ActionAttribute(Name = "新增部门接口", Desc = "新增部门信息")]
        [HttpPost, Route("add")]
        public async Task<IActionResult> add([FromBody]DeptCreateDto dto,[FromHeader(Name = "UserId")]long userId) {
            dto.CreatedBy = dto.UpdatedBy = userId;
            await this._deptService.Add(dto);
            return new OkObjectResult(ResultUtil.Success());
        }

        [ActionAttribute(Name = "更新部门接口", Desc = "更新部门信息")]
        [HttpPost, Route("update")]
        public async Task<IActionResult> update([FromBody]DeptUpdateDto dto,[FromHeader(Name = "UserId")]long userId) {
            dto.UpdatedBy = userId;
            await this._deptService.Update(dto);
            return new OkObjectResult(ResultUtil.Success());
        }

        [ActionAttribute(Name = "部门删除接口", Desc = "删除部门信息")]
        [HttpDelete, Route("{id}")]
        public async Task<IActionResult> delete([FromRoute(Name = "id")]long id,[FromHeader(Name = "UserId")]long userId) {
            await this._deptService.Delete(id,userId);
            return new OkObjectResult(ResultUtil.Success());
        }

    }
}
